package com.logicalthinking.socialmall.service.impl;

import com.github.chenlijia1111.utils.common.Result;
import com.github.chenlijia1111.utils.core.PropertyCheckUtil;
import com.github.chenlijia1111.utils.list.Lists;
import com.logicalthinking.socialmall.common.enums.AuthEnum;
import com.logicalthinking.socialmall.common.vo.requestVo.role.RolePageQueryParams;
import com.logicalthinking.socialmall.common.vo.responseVo.role.AuthEnumVo;
import com.logicalthinking.socialmall.common.vo.responseVo.role.RoleVo;
import com.logicalthinking.socialmall.dao.RoleAuthMapper;
import com.logicalthinking.socialmall.dao.RoleMapper;
import com.logicalthinking.socialmall.entity.Role;
import com.logicalthinking.socialmall.entity.RoleAuth;
import com.logicalthinking.socialmall.service.RoleServiceI;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 角色表
 *
 * @author chenLiJia
 * @since 2020-02-17 14:43:59
 **/
@Service
public class RoleServiceImpl implements RoleServiceI {


    @Resource
    private RoleMapper roleMapper;//角色
    @Resource
    private RoleAuthMapper roleAuthMapper;//角色权限关联


    /**
     * 添加
     *
     * @param params 添加参数
     * @return com.github.chenlijia1111.utils.common.Result
     * @author chenLiJia
     * @since 2020-02-17 14:43:59
     **/
    @Override
    public Result add(Role params) {

        int i = roleMapper.insertSelective(params);
        return i > 0 ? Result.success("操作成功") : Result.failure("操作失败");
    }

    /**
     * 编辑
     *
     * @param params 编辑参数
     * @return com.github.chenlijia1111.utils.common.Result
     * @author chenLiJia
     * @since 2020-02-17 14:43:59
     **/
    @Override
    public Result update(Role params) {

        int i = roleMapper.updateByPrimaryKeySelective(params);
        return i > 0 ? Result.success("操作成功") : Result.failure("操作失败");
    }

    /**
     * 批量添加角色权限关联
     *
     * @param params
     * @param roleId 角色id
     * @return
     */
    @Override
    public Result batchAddRoleAuth(List<Integer> params, Integer roleId) {
        if (Lists.isNotEmpty(params)) {
            List<RoleAuth> collect = params.stream().map(e -> {
                RoleAuth roleAuth = new RoleAuth();
                roleAuth.setRoleId(roleId);
                roleAuth.setAuthId(e);
                return roleAuth;
            }).collect(Collectors.toList());
            roleAuthMapper.batchAdd(collect);
        }
        return Result.success("操作成功");
    }

    /**
     * 查询角色列表
     *
     * @param params
     * @return
     */
    @Override
    public List<RoleVo> listPage(RolePageQueryParams params) {

        List<RoleVo> roleVos = roleMapper.listPage(params);
        getFullInfoWithRoleVo(roleVos);

        return roleVos;
    }

    /**
     * 关联角色权限信息
     *
     * @param list
     */
    @Override
    public void getFullInfoWithRoleVo(List<RoleVo> list) {
        if (Lists.isNotEmpty(list)) {
            //角色id集合
            Set<Integer> roleIdSet = list.stream().map(e -> e.getId()).collect(Collectors.toSet());
            List<RoleAuth> roleAuthList = roleAuthMapper.listByRoleIdSet(roleIdSet);

            //转化为权限枚举类
            if (Lists.isNotEmpty(roleAuthList)) {
                for (RoleVo roleVo : list) {
                    List<AuthEnumVo> collect = roleAuthList.stream().filter(e -> Objects.equals(e.getRoleId(), roleVo.getId())).map(e -> AuthEnumVo.AuthEnumToAuthEnumVo(AuthEnum.authTypeTransferToAuthEnum(e.getAuthId()))).collect(Collectors.toList());
                    roleVo.setAuthList(collect);
                }
            }

        }
    }

    /**
     * 通过角色id删除角色权限
     *
     * @param roleId
     * @return
     */
    @Override
    public Result deleteRoleAuthByRoleId(Integer roleId) {
        if (Objects.isNull(roleId)) {
            return Result.failure("角色id为空");
        }

        RoleAuth roleAuthCondition = new RoleAuth().setRoleId(roleId);
        roleAuthMapper.delete(roleAuthCondition);

        return Result.success("操作成功");
    }

    /**
     * 条件查询
     *
     * @param condition
     * @return
     * @author chenLiJia
     * @since 2020-02-17 14:43:59
     **/
    @Override
    public List<Role> listByCondition(Role condition) {

        PropertyCheckUtil.transferObjectNotNull(condition, true);
        return roleMapper.select(condition);
    }


}
